package com.endpoint.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.endpoint.common.utils.ResultBean;
import com.endpoint.home.dto.BookCommendRespDto;
import com.endpoint.home.entity.HomeBook;
import com.endpoint.home.feign.BookFeignService;
import com.endpoint.home.mapper.HomeBookMapper;
import com.endpoint.home.service.IHomeBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.endpoint.home.to.BookTo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 首页小说设置表 服务实现类
 * </p>
 *
 * @author liusp
 * @since 2025-06-21
 */
@Service
public class HomeBookServiceImpl extends ServiceImpl<HomeBookMapper, HomeBook> implements IHomeBookService {

    @Autowired
    private BookFeignService bookFeignService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //private  Map<String,Object> cache = new HashMap<>();
    @SneakyThrows
    @Override
    public List<BookCommendRespDto> bookCommendList() {
        //在缓存中放json字符串，拿出json字符串，还要逆转为能用的对象类型【序列化与反序列化】
        //JSON好处是跨语言，跨平台兼容。
        //1.获取 字符串类型 对象 并通过key BookCommendJSON ,获取redis中保存数据
        String json =
                stringRedisTemplate.opsForValue().get("BookCommendJSON");
        //2.判断是否wei空  为空 redis中没有数据 ,需要从数据库查询
        if(StringUtils.isEmpty(json)) {
            List<BookCommendRespDto> list = getBookCommendRespDtos();
            json = new ObjectMapper().writeValueAsString(list);

            stringRedisTemplate.opsForValue().set("BookCommendJSON",json,
                    5,TimeUnit.MINUTES);//设置过期时间5分钟
            return list;
        }
        return new ObjectMapper().readValue(json, List.class);
    }

    private @NotNull List<BookCommendRespDto> getBookCommendRespDtos() {
        //1.从home_book查询所有推荐小说
        List<HomeBook> homeBooks = this.baseMapper.selectList(new QueryWrapper<HomeBook>().orderByAsc("sort"));

        if(homeBooks!=null&&homeBooks.size()>0) {
            //2. 获取推荐小说id 封装到list集合
            List<Long> bookIds = homeBooks.stream().map(item -> {
                return item.getBookId();
            }).collect(Collectors.toList());
            //简写 homeBooks.stream().map(HomeBook::getBookId).collect(Collectors.toList());

            //3.据小说ID列表查询相关的小说信息列表
            ResultBean<List<BookTo>> result = bookFeignService.listByIds(bookIds);

            if (result != null && result.getCode() == 200) {
                List<BookTo> books = result.getData();
                // 组装 HomeBookRespDto 列表数据并返回
                if (!CollectionUtils.isEmpty(books)) {
                    //获取map集合  key为 书籍id 值为 Book对象
                    Map<Long, BookTo> bookMap = books.stream()
                            .collect(Collectors.toMap(BookTo::getId, Function.identity()));
                    //通过书籍ID 获取推荐书籍其他属性
                    List<BookCommendRespDto> collect = homeBooks.stream().map(v -> {
                        BookTo book = bookMap.get(v.getBookId());
                        BookCommendRespDto bookRespDto = new BookCommendRespDto();
                        BeanUtils.copyProperties(book, bookRespDto);//复制属性 要优先复制属性再单独设值
                        //设置 类型及书籍id
                        bookRespDto.setType(v.getType());
                        bookRespDto.setBookId(v.getBookId());
                        return bookRespDto;
                    }).collect(Collectors.toList());
                    return collect;
                }
            }
        }
        return Collections.emptyList();
    }


//    private  Map<String,Object> cache = new HashMap<>();
//    @Override
//    public List<BookCommendRespDto> bookCommendList() {
//        List<BookCommendRespDto> bookCommendList =
//                (List<BookCommendRespDto>) cache.get("bookCommendList");
//        if(bookCommendList==null){
//            //执行业务代码
//            cache.put("bookCommendList",collect);
//            return collect;
//        }
//        return bookCommendList;
//    }
}
